<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		// 此处globalData为了演示其作用，不是uView框架的一部分
		globalData: {
		},
		onLaunch() {
		},
		methods: {
			...mapMutations(['$uStore']),
			dark(mediaQueryList) {
				if (mediaQueryList.matches) {
					this.handleDark()
				}
			},
			light(mediaQueryList) {
				if (mediaQueryList.matches) {
					this.handleLight()
				}
			},
			handleDark() {
				uni.setTabBarStyle({
					color: "#aaaaaa",
					selectedColor: "#eeeeee",
					backgroundColor: "#111111",
					borderStyle: "black",
				})
				this.$uStore({
					name: 'vuex_bar_color',
					value: '#111111'
				})
				this.$uStore({
					name: 'vuex_theme',
					value: 'dark'
				})

			},
			handleLight() {
				uni.setTabBarStyle({
					color: '#333333',
					selectedColor: '#111111',
					backgroundColor: '#eeeeee',
					borderStyle: 'white'
				})
				this.$uStore({
					name: 'vuex_bar_color',
					value: '#eeeeee'
				})
				this.$uStore({
					name: 'vuex_theme',
					value: 'light'
				})
			},

		},
		onShow() {
			
			const systemInfo = uni.getSystemInfoSync()
			if (systemInfo.platform == 'ios') {
				if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
					this.handleDark()
				} else {
					this.handleLight()
				}
				const listeners = {
					dark: function(mediaQueryList) {
						if (mediaQueryList.matches) {
							this.handleDark()
						}
					},
					light: function(mediaQueryList) {
						if (mediaQueryList.matches) {
							this.handleLight()
						}
					}
				}
				
				window.matchMedia('(prefers-color-scheme: dark)').addListener(this.dark)
				window.matchMedia('(prefers-color-scheme: light)').addListener(this.light)
			} else {
				this.handleDark()
			}
			
			




			// #ifdef APP-PLUS
			uni.getSystemInfo({
				success: function(res) {
					uni.showModal({
						title: res.screenWidth
					})
					
					// if (res.platform == 'ios') {
					// 	var style = plus.navigator.getUIStyle();
					// 	if ('dark' == style) {
					// 		this.handleDark()
					// 	} else {
					// 		this.handleLight()
					// 	}
					// 	uni.onThemeChange(function(res) {
					// 		if (res.style === 'dark') {
					// 			this.handleDark()
					// 		} else {
					// 			this.handleLight()
					// 		}
					// 	});
					// } else {
					// 	this.handleDark()
					// }
				},
				fail: function(e) {
					uni.showModal({
						title: JSON.stringify(e)
					})
				}
			})
			this.handleDark()
			
			const baseUrl = this.$u.http.config.baseUrl
			const self = this
			if (!baseUrl) {
				uni.getStorage({
					key: 'base_url',
					success: function(res) {
						self.$u.http.config.baseUrl = res.data
					},
					fail: function(e) {
						self.$u.route({
							url: 'pages/cloud/index'
						})
					}
				});
			}
			// #endif
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
</style>
